//
//  STPrivacyAgreementView.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/12/7.
//

import UIKit
import WebKit

class STPrivacyAgreementView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = COLOR_HEX(hexString: "#000000", alpha: 0.4)
        
        let bgView = UIView.init(frame: CGRect.init(x: 16, y: 100, width: kScreenWidth - 32, height: kScreenHeight - 200))
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        addSubview(bgView)
        
        let titleLb = UILabel.init()
        titleLb.textAlignment = .center
        titleLb.text = "隐私协议"
        titleLb.font = UIFont.systemFont(ofSize: 18)
        bgView.addSubview(titleLb)
        titleLb.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(30)
        }
        
        bgView.addSubview(webView)
        webView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLb.snp.bottom).offset(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(-60)
        }
        
        bgView.addSubview(cancleBtn)
        cancleBtn.snp.makeConstraints { (make) in
            make.right.equalTo(bgView.snp.centerX).offset(-20)
            make.width.equalTo(120)
            make.height.equalTo(40)
            make.bottom.equalTo(-10)
        }
        
        bgView.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(bgView.snp.centerX).offset(20)
            make.width.equalTo(120)
            make.height.equalTo(40)
            make.bottom.equalTo(-10)
        }
        
        cancleBtn.addTarget(self, action: #selector(handleCancleAction), for: .touchUpInside)
        confirmBtn.addTarget(self, action: #selector(handleConfirmAction), for: .touchUpInside)
        
        webView.load(URLRequest.init(url: URL.init(string: "\(kServiceHost)/html/privacy.html")!))
    }
    
    @objc func handleCancleAction() {
        exit(0)
    }
    
    @objc func handleConfirmAction() {
        UserDefaults.standard.set("1", forKey: PrivacyAgreementConfirm_Key)
        self.removeFromSuperview()
    }
    
    lazy var webView: WKWebView = {
        let webView = WKWebView.init(frame: CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
        return webView
    }()
    
    lazy var cancleBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("取消", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#E9E9EC", alpha: 1)
        btn.layer.cornerRadius = 12
        btn.setTitleColor(COLOR_HEX(hexString: "#8F92A1", alpha: 1), for: UIControl.State.normal)
        return btn
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("同意", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#3686FF", alpha: 1)
        btn.layer.cornerRadius = 12
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
